//
//  AppDelegate.m
//  Meng
//
//  Created by dllo on 15/9/23.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "AppDelegate.h"
#import "HomeViewController.h"
#import "OneViewController.h"
#import "ListViewController.h"
#import <RESideMenu.h>
#import "LeftViewController.h"
@interface AppDelegate () <RESideMenuDelegate,UIScrollViewDelegate>
@property (nonatomic, retain) UITabBarController *tabBar;
@property (nonatomic, retain) RESideMenu *side;
@property (nonatomic, retain) UIPageControl *page;
@property (nonatomic, retain) UIView *view1;
@end

@implementation AppDelegate

- (void)dealloc
{
    [_tabBar release];
    [_window release];
    [_side release];
    [super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    HomeViewController *home = [[HomeViewController alloc] init];
    UINavigationController *naviHome = [[UINavigationController alloc] initWithRootViewController:home];
    naviHome.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"关注" image:[UIImage imageNamed:@"Home.png"] tag:100];
    
    OneViewController *one = [[OneViewController alloc] init];
    UINavigationController *naviOne = [[UINavigationController alloc] initWithRootViewController:one];
    naviOne.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"腔调" image:[UIImage imageNamed:@"hot.png"] tag:200];
    
    ListViewController *list = [[ListViewController alloc] init];
    UINavigationController *naviList = [[UINavigationController alloc] initWithRootViewController:list];
    naviList.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"专题分类" image:[UIImage imageNamed:@"List.png"] tag:300];
    
    self.tabBar = [[UITabBarController alloc] init];
    self.tabBar.viewControllers = @[naviHome, naviOne, naviList];
//    self.window.rootViewController = self.tabBar;
    
    LeftViewController *left = [[LeftViewController alloc] init];
    self.side = [[RESideMenu alloc] initWithContentViewController:self.tabBar leftMenuViewController:left rightMenuViewController:nil];
    self.side.backgroundImage = [UIImage imageNamed:@"drawer"];
    self.side.menuPreferredStatusBarStyle = 1;
    self.side.delegate = self;
    self.side.contentViewShadowColor = [UIColor blackColor];
    self.side.contentViewShadowOffset = CGSizeMake(0, 0);
    self.side.contentViewShadowOpacity = 0.6;
    self.side.contentViewShadowRadius = 12;
    self.side.contentViewShadowEnabled = YES;
  
    
    
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"everLaunched"]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"everLaunched"];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        
        /* scrollView处理 */
        UIScrollView *myScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 375*WIDTH, 667 *HEIGHT)];
        myScroll.contentSize = CGSizeMake(375*WIDTH * 4, 667*HEIGHT);
        for (int i = 0; i < 4; i++) {
            UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(375*WIDTH * i, 0, 375*WIDTH, 667*HEIGHT)];
            
            myImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d", i + 1]];
            [myScroll addSubview:myImage];
            
        }
        myScroll.pagingEnabled = YES;
        myScroll.showsHorizontalScrollIndicator = NO;
        [self.window addSubview:myScroll];
        myScroll.backgroundColor = [UIColor whiteColor];
        myScroll.bounces = NO;
        
        
        /*  最后一个页面设置一个button */
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [myScroll addSubview:button];
        button.frame = CGRectMake(self.window.frame.size.width * 3 + self.window.frame.size.width / 2 - 80 * WIDTH, self.window.frame.size.height - 110 * HEIGHT, 160 * WIDTH, 50 * HEIGHT);
        button.titleLabel.font = [UIFont systemFontOfSize:20];
        [button addTarget:self action:@selector(click) forControlEvents:(UIControlEventTouchUpInside)];
        button.layer.cornerRadius = 10;
        button.layer.masksToBounds = YES;
        button.layer.borderColor = [UIColor whiteColor].CGColor;
        button.layer.borderWidth = 1;
        [button setTitle:@"Go" forState:UIControlStateNormal];
        button.backgroundColor = [UIColor colorWithRed:188 / 255.f green:228 / 255.f blue:219 / 255.f alpha:1.000];
        
        /*  签订协议 */
        myScroll.delegate = self;
        
        
        /* 页面处理 */
        self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 620 * HEIGHT, 375*WIDTH, 30 * HEIGHT)];
        self.page.numberOfPages = 4;
        self.page.currentPage = 0;
        self.page.backgroundColor = [UIColor clearColor];
        self.page.currentPageIndicatorTintColor = [UIColor cyanColor];
        self.page.pageIndicatorTintColor = [UIColor whiteColor];
        [self.window addSubview:self.page];
    } else {
        self.window.rootViewController = self.side;
        
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center addObserver:self selector:@selector(change:) name:@"nightModel" object:nil];
        
        self.view1 = [[UIView alloc] initWithFrame:self.window.frame];
        [self.window addSubview:self.view1];
        [_view1 release];
        self.view1.userInteractionEnabled = NO;
        self.view1.backgroundColor = [UIColor blackColor];
        self.view1.alpha = 0;
        
        
    }
    
    
    
   
    
    
    
    
    
    
    [left release];
    [_side release];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
    [home release];
    [_window release];
    [naviHome release];
    [naviHome.tabBarItem release];
    
    [one release];
    [naviOne release];
    [naviOne.tabBarItem release];
    
    [list release];
    [naviList release];
    [naviList.tabBarItem release];
    [_tabBar release];
    

    return YES;
}
- (void)change:(NSNotification *)notification
{
    self.view1.alpha = [notification.object floatValue];
}
- (void)click
{
    self.window.rootViewController = self.side;
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(change:) name:@"nightModel" object:nil];
    
    self.view1 = [[UIView alloc] initWithFrame:self.window.frame];
    [self.window addSubview:self.view1];
    [_view1 release];
    self.view1.userInteractionEnabled = NO;
    self.view1.backgroundColor = [UIColor blackColor];
    self.view1.alpha = 0;

}
/* 页面移动处理 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{
    self.page.currentPage = scrollView.contentOffset.x / scrollView.window.frame.size.width;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
